package com.iknow.activity;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.User;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.data.QingBo;
import com.iknow.data.SubscribeTag;
import com.iknow.db.IKnowDataBase;
import com.iknow.http.HttpException;
import com.iknow.task.CommonTask.QingboTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.QingBoAdapter;
import com.iknow.util.StringUtil;
import com.iknow.view.widget.PullToRefreshListView;
import com.iknow.view.widget.PullToRefreshListView.OnRefreshListener;
import com.mobclick.android.MobclickAgent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class MySubscribeActivity extends Activity
{
  private AbsListView.OnScrollListener ListViewOnScrollListener;
  private PullToRefreshListView.OnRefreshListener ListViewRefreshListener;
  private View.OnClickListener ManagerTagButtonClickListener;
  private final String TAG = "MySubscribeActivity";
  private AdapterView.OnItemClickListener listItemClickListener;
  protected QingBoAdapter mAdapter;
  protected String mAuthorID;
  protected SimpleDateFormat mDateFormat;
  protected Button mGetMoreButton;
  protected TaskListener mGetMoreListener;
  private Button mGetTagButton;
  private FrameLayout mIntroduceLayout;
  protected PullToRefreshListView mListView;
  protected View mListViewFooter;
  private View mListViewHeader;
  protected LocalDataTask mLocalDataTask;
  protected int mLocalOffset = 0;
  protected Button mManagerTagButton;
  protected CommonTask.QingboTask mTask;
  protected TaskListener mTaskListener;
  private User mUser;

  public MySubscribeActivity()
  {
    this.mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    this.ManagerTagButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        Intent localIntent = new Intent(this.this$0, FlagActivity.class);
        this.this$0.startActivity(localIntent);
      }
    };
    this.ListViewRefreshListener = new PullToRefreshListView.OnRefreshListener(this)
    {
      public void onRefresh()
      {
        MySubscribeActivity.access$0(this.this$0);
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        QingBo localQingBo = this.this$0.mAdapter.getItem(paramInt - 2);
        if (localQingBo == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, PageActivity.class);
          localIntent.putExtra("item", localQingBo);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.ListViewOnScrollListener = new AbsListView.OnScrollListener(this)
    {
      public void onScroll(, int paramInt1, int paramInt2, int paramInt3)
      {
      }

      public void onScrollStateChanged(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 2:
        case 1:
        case 0:
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              this.this$0.mAdapter.setScrolling(true);
            }
            this.this$0.mAdapter.setScrolling(true);
          }
          this.this$0.mAdapter.scrollIdle(paramAbsListView);
        }
      }
    };
    this.mGetMoreListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "getMore";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        this.this$0.mGetMoreButton.setText("更多");
        this.this$0.mAdapter.notifyDataSetChanged();
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (paramGenericTask instanceof CommonTask.QingboTask)
        {
          List localList = (List)paramObject;
          this.this$0.mAdapter.addQingboAtFoot(localList);
          MySubscribeActivity.access$1(this.this$0, localList.size());
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "QingboTask";
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramGenericTask instanceof MySubscribeActivity.LocalDataTask)
        {
          this.this$0.mListView.onRefreshComplete();
          String str2 = IKnow.mSystemConfig.getString("MySubscribeActivity");
          if (!(StringUtil.isEmpty(str2)))
            this.this$0.mListView.setLastUpdated("更新于" + str2);
          this.this$0.mListView.prepareForRefresh();
          this.this$0.mListView.onRefresh();
        }
        if ((paramGenericTask instanceof CommonTask.QingboTask) && (paramTaskResult == TaskResult.OK))
        {
          String str1 = this.this$0.mDateFormat.format(new Date(System.currentTimeMillis()));
          IKnow.mSystemConfig.setString("MySubscribeActivity", str1);
          this.this$0.mListView.setLastUpdated("更新于" + str1);
        }
        if (this.this$0.mAdapter.getCount() > 0)
          MySubscribeActivity.access$2(this.this$0);
        this.this$0.mAdapter.notifyDataSetChanged();
        this.this$0.mListView.onRefreshComplete();
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (paramGenericTask instanceof MySubscribeActivity.LocalDataTask)
        {
          List localList2 = (List)paramObject;
          if ((localList2 != null) && (localList2.size() > 0))
            this.this$0.mListViewFooter.setVisibility(0);
          this.this$0.mAdapter.addQingboAtHead(localList2);
        }
        while (true)
        {
          do
            return;
          while (!(paramGenericTask instanceof CommonTask.QingboTask));
          List localList1 = (List)paramObject;
          if ((localList1 != null) && (localList1.size() > 0))
            this.this$0.mListViewFooter.setVisibility(0);
          this.this$0.mAdapter.clearQingboList(0, localList1.size());
          this.this$0.mAdapter.addQingboAtHead(localList1);
          MySubscribeActivity.access$1(this.this$0, localList1.size());
        }
      }
    };
  }

  private void doNeedMore()
  {
    List localList;
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    do
    {
      do
        return;
      while (this.mUser == null);
      this.mGetMoreButton.setText("正在载入数据...");
      this.mTask = new CommonTask.QingboTask();
      this.mTask.setListener(this.mGetMoreListener);
      this.mTask.setAdapter(this.mAdapter);
      localList = IKnow.mIKnowDataBase.getSubscribeTags(this.mUser.getUID());
    }
    while ((localList == null) || (localList.size() == 0));
    String str = "";
    Iterator localIterator = localList.iterator();
    while (true)
    {
      while (!(localIterator.hasNext()))
      {
        TaskParams localTaskParams = new TaskParams();
        localTaskParams.put("tags", str);
        localTaskParams.put("offset", Integer.valueOf(this.mAdapter.getCount()));
        localTaskParams.put("length", Integer.valueOf(20));
        CommonTask.QingboTask localQingboTask = this.mTask;
        TaskParams[] arrayOfTaskParams = new TaskParams[1];
        arrayOfTaskParams[0] = localTaskParams;
        localQingboTask.execute(arrayOfTaskParams);
      }
      SubscribeTag localSubscribeTag = (SubscribeTag)localIterator.next();
      str = str + localSubscribeTag.getTagName() + ",";
    }
  }

  private void getLocalData()
  {
    if ((this.mLocalDataTask != null) && (this.mLocalDataTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mListView.prepareForRefresh();
      this.mLocalDataTask = new LocalDataTask(this, null);
      this.mLocalDataTask.setListener(this.mTaskListener);
      this.mLocalDataTask.execute(new TaskParams[0]);
    }
  }

  private void initView()
  {
    setContentView(2130903095);
    this.mListView = ((PullToRefreshListView)findViewById(2131296321));
    this.mListView.setOnRefreshListener(this.ListViewRefreshListener);
    this.mListViewHeader = getLayoutInflater().inflate(2130903096, null);
    this.mManagerTagButton = ((Button)this.mListViewHeader.findViewById(2131296461));
    this.mManagerTagButton.setOnClickListener(this.ManagerTagButtonClickListener);
    this.mListView.addHeaderView(this.mListViewHeader);
    setupListViewFooter();
    this.mGetTagButton = ((Button)findViewById(2131296460));
    this.mGetTagButton.setOnClickListener(this.ManagerTagButtonClickListener);
    this.mIntroduceLayout = ((FrameLayout)findViewById(2131296459));
  }

  private void refreshData()
  {
    List localList;
    if (this.mUser == null)
    {
      this.mAdapter.clearQingBoList();
      this.mListView.onRefreshComplete();
      this.mIntroduceLayout.setVisibility(0);
      this.mManagerTagButton.setVisibility(8);
      this.mListView.setVisibility(8);
    }
    while (true)
    {
      return;
      localList = IKnow.mIKnowDataBase.getSubscribeTags(this.mUser.getUID());
      if ((localList != null) && (localList.size() != 0))
        break;
      this.mAdapter.clearQingBoList();
      this.mListView.onRefreshComplete();
      this.mIntroduceLayout.setVisibility(0);
      this.mManagerTagButton.setVisibility(8);
      this.mListView.setVisibility(8);
    }
    String str1 = "";
    Iterator localIterator = localList.iterator();
    while (true)
    {
      while (true)
      {
        do
          if (localIterator.hasNext())
            break label302;
        while ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
        String str2 = IKnow.mSystemConfig.getString("MySubscribeActivity");
        this.mTask = new CommonTask.QingboTask();
        this.mTask.setListener(this.mTaskListener);
        this.mTask.setAdapter(this.mAdapter);
        TaskParams localTaskParams = new TaskParams();
        localTaskParams.put("offset", Integer.valueOf(0));
        localTaskParams.put("length", Integer.valueOf(20));
        localTaskParams.put("tags", str1);
        if (!(StringUtil.isEmpty(str2)))
          this.mListView.setLastUpdated("更新于" + str2);
        CommonTask.QingboTask localQingboTask = this.mTask;
        TaskParams[] arrayOfTaskParams = new TaskParams[1];
        arrayOfTaskParams[0] = localTaskParams;
        localQingboTask.execute(arrayOfTaskParams);
      }
      label302: SubscribeTag localSubscribeTag = (SubscribeTag)localIterator.next();
      str1 = str1 + localSubscribeTag.getTagName() + ",";
    }
  }

  private void setupListViewFooter()
  {
    if (this.mListViewFooter != null);
    while (true)
    {
      return;
      this.mListViewFooter = getLayoutInflater().inflate(2130903105, null);
      this.mGetMoreButton = ((Button)this.mListViewFooter.findViewById(2131296480));
      this.mGetMoreButton.setOnClickListener(new View.OnClickListener(this)
      {
        public void onClick()
        {
          MySubscribeActivity.access$4(this.this$0);
        }
      });
      this.mListView.addFooterView(this.mListViewFooter);
      this.mListView.setOnItemClickListener(this.listItemClickListener);
      this.mListView.setOnScrollListener(this.ListViewOnScrollListener);
      this.mListView.setAdapter(this.mAdapter);
      this.mListViewFooter.setVisibility(8);
    }
  }

  private void showNewQingboTips(int paramInt)
  {
    if (paramInt == 0)
      Toast.makeText(this, "没有新内容，先逛逛其他的吧", 0).show();
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    this.mAdapter = new QingBoAdapter(this, getLayoutInflater());
    this.mAdapter.setShowTagView(true);
    initView();
    getLocalData();
  }

  protected void onDestroy()
  {
    this.mUser = null;
    this.mAdapter.clearQingBoList();
    super.onDestroy();
  }

  protected void onPause()
  {
    this.mUser = null;
    MobclickAgent.onPause(this);
    super.onPause();
  }

  protected void onResume()
  {
    this.mUser = IKnow.mIKnowDataBase.getUser();
    if ((this.mUser == null) || (!(IKnow.mIKnowDataBase.haveSubscribeTag(this.mUser.getUID()))))
    {
      this.mIntroduceLayout.setVisibility(0);
      this.mManagerTagButton.setVisibility(8);
      this.mListView.setVisibility(8);
    }
    while (true)
    {
      MobclickAgent.onResume(this);
      MobclickAgent.setSessionContinueMillis(600000L);
      super.onResume();
      return;
      this.mIntroduceLayout.setVisibility(8);
      this.mManagerTagButton.setVisibility(0);
      this.mListView.setVisibility(0);
    }
  }

  public boolean onSearchRequested()
  {
    boolean bool;
    if (getParent() instanceof TabActivity)
      bool = getParent().onSearchRequested();
    while (true)
    {
      return bool;
      bool = super.onSearchRequested();
    }
  }

  public void onTabClick()
  {
    this.mListView.setSelection(1);
  }

  private class LocalDataTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      String str;
      Iterator localIterator;
      if (MySubscribeActivity.access$3(this.this$0) != null)
      {
        List localList1 = IKnow.mIKnowDataBase.getSubscribeTags(MySubscribeActivity.access$3(this.this$0).getUID());
        if ((localList1 == null) || (localList1.size() == 0))
        {
          localTaskResult = TaskResult.OK;
          return localTaskResult;
        }
        str = "";
        localIterator = localList1.iterator();
        if (localIterator.hasNext())
          break label106;
      }
      try
      {
        while (true)
        {
          List localList2 = IKnow.mApi.getQingboListByTagLocal(str, 0, 20);
          Object[] arrayOfObject = new Object[1];
          arrayOfObject[0] = localList2;
          publishProgress(arrayOfObject);
          localTaskResult = TaskResult.OK;
        }
        label106: SubscribeTag localSubscribeTag = (SubscribeTag)localIterator.next();
        str = str + localSubscribeTag.getTagName() + ",";
      }
      catch (HttpException localHttpException)
      {
        while (true)
          localTaskResult = TaskResult.FAILED;
      }
    }
  }
}